<?php
/**
 * 餐厅合并提交页面
 *
 * @copyright shaocong
 * @since 2012-06-05
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{	
	global $g_cgival, $g_pro, $g_show;
	cgi::get($g_cgival["RstrtID"], "RstrtID", "");
	$g_cgival["RstrtID"] = string::un_script_code($g_cgival["RstrtID"]);
	$g_cgival["RstrtID"] = string::un_html($g_cgival["RstrtID"]);
	
	cgi::get($g_cgival["RstrtName"], "RstrtName", "");
	$g_cgival["RstrtName"] = string::un_script_code($g_cgival["RstrtName"]);
	$g_cgival["RstrtName"] = string::un_html($g_cgival["RstrtName"]);
	
	cgi::get($g_cgival["ZDRstrtID"], "ZDRstrtID", 0);
	$g_cgival["ZDRstrtID"] = intval($g_cgival["ZDRstrtID"]);

		
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	//$_GET['db_debug'] = true;
	$g_cgival['RstrtID'] = $g_pro['RstrtID'] = trim($g_cgival['RstrtID'],',');
	$g_cgival['RstrtName'] = trim($g_cgival['RstrtName']);
	if(empty($g_cgival['RstrtID']))
	{
		throw new Exception("请选择餐厅");
	}
	
	$myobj = new mod_web_rstrt();
		
	//判断总店品牌是否存在
	
	if($g_cgival["ZDRstrtID"] == 0)
	{
		$argsP[] = "RstrtName = '{$g_cgival['RstrtName']}'";
		$argsP[] = "RstrtPID = '0'";
		$argsP[] = "RstrtChain = '1'";
	}else
	{
		$argsP[] = "RstrtID = '{$g_cgival['ZDRstrtID']}'";
	}
	
	$rstrtPData = $myobj->admin_get_rstrt_list(1,1,$argsP,'RstrtID desc');
	if($rstrtPData['count'] > 0)
	{
		$mydata['result'] = 1;
		$mydata['id'] = $rstrtPData['data'][0]['RstrtID'];
	}else {
		throw new Exception("总店不存在");
	}	
	
	//总店名称
	if(empty($g_cgival['RstrtName'])) { $g_cgival['RstrtName'] = $rstrtPData['data'][0]['RstrtName'];}
	
	

	$args[] = "RstrtID in({$g_cgival['RstrtID']})";
	$rstrtData = $myobj->admin_get_rstrt_list(-1,-1,$args,'RstrtID desc');
	$rstrtInfo = $rstrtData['data'][0];
	
    if(!empty($rstrtInfo))
    {
		if($mydata['result'] == '1')//更新分店
		{
			foreach ($rstrtData['data'] as $k=>$rstrtVal)
			{
				//if($rstrtVal['RstrtID'] == $mydata['id']) continue;
				$g_pro['RstrtChain']['RstrtPID'] = ($rstrtVal['RstrtID'] == $mydata['id']) ? '-1' : $mydata['id'];//品牌餐厅ID
				$g_pro['RstrtChain']['RstrtChainName'] = ($rstrtVal['RstrtID'] == $mydata['id']) ? "" : (!empty($rstrtVal['RstrtChainName'])) ? $rstrtVal['RstrtChainName'] : $rstrtVal['RstrtName'];//餐厅分店名称
				$g_pro['RstrtChain']['RstrtName'] = $g_cgival['RstrtName'];//餐厅名称
				$g_pro['RstrtChain']['RstrtShow'] = 1;
				$g_pro['RstrtChain']['RstrtChain'] = 1;
				$g_pro['RstrtChain']['RstrtID'] = $rstrtVal['RstrtID'];
				$g_pro['RstrtChain']['site_id'] = $rstrtVal['site_id'];
				$myRstrtP = $myobj->update_rstrt_show($g_pro['RstrtChain'],$g_pro['RstrtChain']['RstrtID']);	
			}
			if($myRstrtP['result'] == 1)
			{
				$g_show['result'] = 0;
			}
			else 
			{
				throw new Exception("操作失败");
			}
		}
		else 
		{
			throw new Exception("操作失败");
		}
    }
}

try
{
	admin_check_login();
//	admin_check_user_priv("ajax_rstrt_merge.php");
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show['result'] = 1;
	$g_show['error'] = $e->getMessage();
}

show_pro();
exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	if(isset($_GET['debug']))
		print_r($g_show);
	echo output::json('json',$g_show);
	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}

?>